#!/bin/sh
SCRIPTDIR=$(cd `dirname $0`; pwd)
echo $SCRIPTDIR
SERVICE_NAME=${SCRIPTDIR##*/}

HOME=$SCRIPTDIR
PATH_TO_JAR=$HOME/oam_rtc-0.0.1-SNAPSHOT.jar
PID_PATH_NAME=/$HOME/${SERVICE_NAME}.pid
LOG=$HOME/log/info.log
ERROR_LOG=$HOME/log/err.log
CONFIG=$HOME/application.properties
case $1 in
    start)
        echo "Starting $SERVICE_NAME ..."
        if [ ! -f $PID_PATH_NAME ]; then
            cd $HOME
            nohup java -jar $PATH_TO_JAR --spring.config.location=$CONFIG > $LOG 2> $ERROR_LOG &
            #java -jar $PATH_TO_JAR --spring.config.location=$CONFIG  >/dev/null 2>&1 &
            echo $! > $PID_PATH_NAME
            echo "$SERVICE_NAME started ..."
        else
            echo "$SERVICE_NAME is already running ..."
        fi
    ;;
    stop)
        if [ -f $PID_PATH_NAME ]; then
            PID=$(cat $PID_PATH_NAME);
            echo "$SERVICE_NAME stoping ..."
            kill $PID;
            echo "$SERVICE_NAME stopped ..."
            rm $PID_PATH_NAME
        else
            echo "$SERVICE_NAME is not running ..."
        fi
    ;;
    stat)
        if [ -f $PID_PATH_NAME ]; then
            echo "$SERVICE_NAME is running ..."
        else
            echo "$SERVICE_NAME is not running ..."
        fi
    ;;
    restart)
        if [ -f $PID_PATH_NAME ]; then
            PID=$(cat $PID_PATH_NAME);
            echo "$SERVICE_NAME stopping ...";
            kill $PID;
            echo "$SERVICE_NAME stopped ...";
            rm $PID_PATH_NAME
            echo "$SERVICE_NAME starting ..."
            cd $HOME
            #nohup java -jar $PATH_TO_JAR --spring.config.location=$CONFIG > $LOG 2> $ERROR_LOG &
            nohup java -jar $PATH_TO_JAR --spring.config.location=$CONFIG > /dev/null 2>&1 &
            echo $! > $PID_PATH_NAME
            echo "$SERVICE_NAME started ..."
        else
            echo "$SERVICE_NAME is not running ..."
        fi
    ;;
    *)
        echo "---------------------------------------"
        echo "usage: $SERVICE_NAME start|stop|stat|restart"
        echo "---------------------------------------"
    ;;
esac

